package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.FacultyDAO;
import edu.unf.cen6940.dao.FacultyresearchDAO;
import edu.unf.cen6940.dao.FacultyresearchtostudentDAO;
import edu.unf.cen6940.dao.ThesiscommitteeDAO;

import edu.unf.cen6940.domain.Faculty;
import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Facultyresearchtostudent;
import edu.unf.cen6940.domain.Thesiscommittee;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Faculty entities
 * 
 */

@Service("FacultyService")
@Transactional
public class FacultyServiceImpl implements FacultyService {

	/**
	 * DAO injected by Spring that manages Faculty entities
	 * 
	 */
	@Autowired
	private FacultyDAO facultyDAO;

	/**
	 * DAO injected by Spring that manages Facultyresearch entities
	 * 
	 */
	@Autowired
	private FacultyresearchDAO facultyresearchDAO;

	/**
	 * DAO injected by Spring that manages Facultyresearchtostudent entities
	 * 
	 */
	@Autowired
	private FacultyresearchtostudentDAO facultyresearchtostudentDAO;

	/**
	 * DAO injected by Spring that manages Thesiscommittee entities
	 * 
	 */
	@Autowired
	private ThesiscommitteeDAO thesiscommitteeDAO;

	/**
	 * Instantiates a new FacultyServiceImpl.
	 *
	 */
	public FacultyServiceImpl() {
	}

	/**
	 * Return all Faculty entity
	 * 
	 */
	@Transactional
	public List<Faculty> findAllFacultys(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Faculty>(facultyDAO.findAllFacultys(startResult, maxRows));
	}

	/**
	 * Delete an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyFacultyresearchs(Integer faculty_facultyId, Integer related_facultyresearchs_facultyResearchId) {
		Facultyresearch related_facultyresearchs = facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearchs_facultyResearchId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_facultyresearchs.setFaculty(null);
		faculty.getFacultyresearchs().remove(related_facultyresearchs);

		facultyresearchDAO.remove(related_facultyresearchs);
		facultyresearchDAO.flush();

		return faculty;
	}

	/**
	 * Delete an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyFacultyresearchtostudents(Integer faculty_facultyId, Integer related_facultyresearchtostudents_facultyResearchId, Integer related_facultyresearchtostudents_studentId, Integer related_facultyresearchtostudents_facultyId) {
		Facultyresearchtostudent related_facultyresearchtostudents = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(related_facultyresearchtostudents_facultyResearchId, related_facultyresearchtostudents_studentId, related_facultyresearchtostudents_facultyId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_facultyresearchtostudents.setFaculty(null);
		faculty.getFacultyresearchtostudents().remove(related_facultyresearchtostudents);

		facultyresearchtostudentDAO.remove(related_facultyresearchtostudents);
		facultyresearchtostudentDAO.flush();

		return faculty;
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public void saveFaculty(Faculty faculty) {
		Faculty existingFaculty = facultyDAO.findFacultyByPrimaryKey(faculty.getFacultyId());

		if (existingFaculty != null) {
			if (existingFaculty != faculty) {
				existingFaculty.setFacultyId(faculty.getFacultyId());
				existingFaculty.setNnumber(faculty.getNnumber());
				existingFaculty.setFirstName(faculty.getFirstName());
				existingFaculty.setLastName(faculty.getLastName());
				existingFaculty.setEmail(faculty.getEmail());
				existingFaculty.setUrl(faculty.getUrl());
				existingFaculty.setActive(faculty.getActive());
			}
			faculty = facultyDAO.store(existingFaculty);
		} else {
			faculty = facultyDAO.store(faculty);
		}
		facultyDAO.flush();
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyThesiscommitteesForCommitteeMember4(Integer faculty_facultyId, Integer related_thesiscommitteesforcommitteemember4_thesisCommitteeId) {
		Thesiscommittee related_thesiscommitteesforcommitteemember4 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember4_thesisCommitteeId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_thesiscommitteesforcommitteemember4.setFacultyByCommitteeMember1(null);
		faculty.getThesiscommitteesForCommitteeMember1().remove(related_thesiscommitteesforcommitteemember4);

		thesiscommitteeDAO.remove(related_thesiscommitteesforcommitteemember4);
		thesiscommitteeDAO.flush();

		return faculty;
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyThesiscommitteesForCommitteeMember5(Integer faculty_facultyId, Integer related_thesiscommitteesforcommitteemember5_thesisCommitteeId) {
		Thesiscommittee related_thesiscommitteesforcommitteemember5 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember5_thesisCommitteeId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_thesiscommitteesforcommitteemember5.setFacultyByCommitteeMember1(null);
		faculty.getThesiscommitteesForCommitteeMember1().remove(related_thesiscommitteesforcommitteemember5);

		thesiscommitteeDAO.remove(related_thesiscommitteesforcommitteemember5);
		thesiscommitteeDAO.flush();

		return faculty;
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyThesiscommitteesForCommitteeMember1(Integer faculty_facultyId, Integer related_thesiscommitteesforcommitteemember1_thesisCommitteeId) {
		Thesiscommittee related_thesiscommitteesforcommitteemember1 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember1_thesisCommitteeId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_thesiscommitteesforcommitteemember1.setFacultyByCommitteeMember1(null);
		faculty.getThesiscommitteesForCommitteeMember1().remove(related_thesiscommitteesforcommitteemember1);

		thesiscommitteeDAO.remove(related_thesiscommitteesforcommitteemember1);
		thesiscommitteeDAO.flush();

		return faculty;
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public void deleteFaculty(Faculty faculty) {
		facultyDAO.remove(faculty);
		facultyDAO.flush();
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyThesiscommitteesForCommitteeMember5(Integer facultyId, Thesiscommittee related_thesiscommitteesforcommitteemember5) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Thesiscommittee existingthesiscommitteesForCommitteeMember5 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember5.getThesisCommitteeId());

		// copy into the existing record to preserve existing relationships
		if (existingthesiscommitteesForCommitteeMember5 != null) {
			existingthesiscommitteesForCommitteeMember5.setThesisCommitteeId(related_thesiscommitteesforcommitteemember5.getThesisCommitteeId());
			related_thesiscommitteesforcommitteemember5 = existingthesiscommitteesForCommitteeMember5;
		} else {
			related_thesiscommitteesforcommitteemember5 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember5);
			thesiscommitteeDAO.flush();
		}

		related_thesiscommitteesforcommitteemember5.setFacultyByCommitteeMember1(faculty);
		faculty.getThesiscommitteesForCommitteeMember1().add(related_thesiscommitteesforcommitteemember5);
		related_thesiscommitteesforcommitteemember5 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember5);
		thesiscommitteeDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyThesiscommitteesForChair(Integer faculty_facultyId, Integer related_thesiscommitteesforchair_thesisCommitteeId) {
		Thesiscommittee related_thesiscommitteesforchair = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforchair_thesisCommitteeId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_thesiscommitteesforchair.setFacultyByCommitteeMember1(null);
		faculty.getThesiscommitteesForCommitteeMember1().remove(related_thesiscommitteesforchair);

		thesiscommitteeDAO.remove(related_thesiscommitteesforchair);
		thesiscommitteeDAO.flush();

		return faculty;
	}

	/**
	 * Return a count of all Faculty entity
	 * 
	 */
	@Transactional
	public Integer countFacultys() {
		return ((Long) facultyDAO.createQuerySingleResult("select count(o) from Faculty o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public Faculty findFacultyByPrimaryKey(Integer facultyId) {
		return facultyDAO.findFacultyByPrimaryKey(facultyId);
	}

	/**
	 * Save an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyFacultyresearchtostudents(Integer facultyId, Facultyresearchtostudent related_facultyresearchtostudents) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Facultyresearchtostudent existingfacultyresearchtostudents = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(related_facultyresearchtostudents.getFacultyResearchId(), related_facultyresearchtostudents.getStudentId(), related_facultyresearchtostudents.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyresearchtostudents != null) {
			existingfacultyresearchtostudents.setFacultyResearchId(related_facultyresearchtostudents.getFacultyResearchId());
			existingfacultyresearchtostudents.setStudentId(related_facultyresearchtostudents.getStudentId());
			existingfacultyresearchtostudents.setFacultyId(related_facultyresearchtostudents.getFacultyId());
			related_facultyresearchtostudents = existingfacultyresearchtostudents;
		}

		related_facultyresearchtostudents.setFaculty(faculty);
		faculty.getFacultyresearchtostudents().add(related_facultyresearchtostudents);
		related_facultyresearchtostudents = facultyresearchtostudentDAO.store(related_facultyresearchtostudents);
		facultyresearchtostudentDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}

	/**
	 * Save an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyFacultyresearchs(Integer facultyId, Facultyresearch related_facultyresearchs) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Facultyresearch existingfacultyresearchs = facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearchs.getFacultyResearchId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyresearchs != null) {
			existingfacultyresearchs.setFacultyResearchId(related_facultyresearchs.getFacultyResearchId());
			existingfacultyresearchs.setTitle(related_facultyresearchs.getTitle());
			existingfacultyresearchs.setResearchDescription(related_facultyresearchs.getResearchDescription());
			existingfacultyresearchs.setResearchStatus(related_facultyresearchs.getResearchStatus());
			existingfacultyresearchs.setMinumumRequirements(related_facultyresearchs.getMinumumRequirements());
			related_facultyresearchs = existingfacultyresearchs;
		}

		related_facultyresearchs.setFaculty(faculty);
		faculty.getFacultyresearchs().add(related_facultyresearchs);
		related_facultyresearchs = facultyresearchDAO.store(related_facultyresearchs);
		facultyresearchDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyThesiscommitteesForCommitteeMember3(Integer facultyId, Thesiscommittee related_thesiscommitteesforcommitteemember3) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Thesiscommittee existingthesiscommitteesForCommitteeMember3 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember3.getThesisCommitteeId());

		// copy into the existing record to preserve existing relationships
		if (existingthesiscommitteesForCommitteeMember3 != null) {
			existingthesiscommitteesForCommitteeMember3.setThesisCommitteeId(related_thesiscommitteesforcommitteemember3.getThesisCommitteeId());
			related_thesiscommitteesforcommitteemember3 = existingthesiscommitteesForCommitteeMember3;
		} else {
			related_thesiscommitteesforcommitteemember3 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember3);
			thesiscommitteeDAO.flush();
		}

		related_thesiscommitteesforcommitteemember3.setFacultyByCommitteeMember1(faculty);
		faculty.getThesiscommitteesForCommitteeMember1().add(related_thesiscommitteesforcommitteemember3);
		related_thesiscommitteesforcommitteemember3 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember3);
		thesiscommitteeDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyThesiscommitteesForCommitteeMember1(Integer facultyId, Thesiscommittee related_thesiscommitteesforcommitteemember1) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Thesiscommittee existingthesiscommitteesForCommitteeMember1 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember1.getThesisCommitteeId());

		// copy into the existing record to preserve existing relationships
		if (existingthesiscommitteesForCommitteeMember1 != null) {
			existingthesiscommitteesForCommitteeMember1.setThesisCommitteeId(related_thesiscommitteesforcommitteemember1.getThesisCommitteeId());
			related_thesiscommitteesforcommitteemember1 = existingthesiscommitteesForCommitteeMember1;
		} else {
			related_thesiscommitteesforcommitteemember1 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember1);
			thesiscommitteeDAO.flush();
		}

		related_thesiscommitteesforcommitteemember1.setFacultyByCommitteeMember1(faculty);
		faculty.getThesiscommitteesForCommitteeMember1().add(related_thesiscommitteesforcommitteemember1);
		related_thesiscommitteesforcommitteemember1 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember1);
		thesiscommitteeDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyThesiscommitteesForCommitteeMember2(Integer faculty_facultyId, Integer related_thesiscommitteesforcommitteemember2_thesisCommitteeId) {
		Thesiscommittee related_thesiscommitteesforcommitteemember2 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember2_thesisCommitteeId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_thesiscommitteesforcommitteemember2.setFacultyByCommitteeMember1(null);
		faculty.getThesiscommitteesForCommitteeMember1().remove(related_thesiscommitteesforcommitteemember2);

		thesiscommitteeDAO.remove(related_thesiscommitteesforcommitteemember2);
		thesiscommitteeDAO.flush();

		return faculty;
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyThesiscommitteesForCommitteeMember2(Integer facultyId, Thesiscommittee related_thesiscommitteesforcommitteemember2) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Thesiscommittee existingthesiscommitteesForCommitteeMember2 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember2.getThesisCommitteeId());

		// copy into the existing record to preserve existing relationships
		if (existingthesiscommitteesForCommitteeMember2 != null) {
			existingthesiscommitteesForCommitteeMember2.setThesisCommitteeId(related_thesiscommitteesforcommitteemember2.getThesisCommitteeId());
			related_thesiscommitteesforcommitteemember2 = existingthesiscommitteesForCommitteeMember2;
		} else {
			related_thesiscommitteesforcommitteemember2 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember2);
			thesiscommitteeDAO.flush();
		}

		related_thesiscommitteesforcommitteemember2.setFacultyByCommitteeMember1(faculty);
		faculty.getThesiscommitteesForCommitteeMember1().add(related_thesiscommitteesforcommitteemember2);
		related_thesiscommitteesforcommitteemember2 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember2);
		thesiscommitteeDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty deleteFacultyThesiscommitteesForCommitteeMember3(Integer faculty_facultyId, Integer related_thesiscommitteesforcommitteemember3_thesisCommitteeId) {
		Thesiscommittee related_thesiscommitteesforcommitteemember3 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember3_thesisCommitteeId, -1, -1);

		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(faculty_facultyId, -1, -1);

		related_thesiscommitteesforcommitteemember3.setFacultyByCommitteeMember1(null);
		faculty.getThesiscommitteesForCommitteeMember1().remove(related_thesiscommitteesforcommitteemember3);

		thesiscommitteeDAO.remove(related_thesiscommitteesforcommitteemember3);
		thesiscommitteeDAO.flush();

		return faculty;
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyThesiscommitteesForChair(Integer facultyId, Thesiscommittee related_thesiscommitteesforchair) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Thesiscommittee existingthesiscommitteesForChair = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforchair.getThesisCommitteeId());

		// copy into the existing record to preserve existing relationships
		if (existingthesiscommitteesForChair != null) {
			existingthesiscommitteesForChair.setThesisCommitteeId(related_thesiscommitteesforchair.getThesisCommitteeId());
			related_thesiscommitteesforchair = existingthesiscommitteesForChair;
		} else {
			related_thesiscommitteesforchair = thesiscommitteeDAO.store(related_thesiscommitteesforchair);
			thesiscommitteeDAO.flush();
		}

		related_thesiscommitteesforchair.setFacultyByCommitteeMember1(faculty);
		faculty.getThesiscommitteesForCommitteeMember1().add(related_thesiscommitteesforchair);
		related_thesiscommitteesforchair = thesiscommitteeDAO.store(related_thesiscommitteesforchair);
		thesiscommitteeDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}

	/**
	 * Load an existing Faculty entity
	 * 
	 */
	@Transactional
	public Set<Faculty> loadFacultys() {
		return facultyDAO.findAllFacultys();
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Faculty saveFacultyThesiscommitteesForCommitteeMember4(Integer facultyId, Thesiscommittee related_thesiscommitteesforcommitteemember4) {
		Faculty faculty = facultyDAO.findFacultyByPrimaryKey(facultyId, -1, -1);
		Thesiscommittee existingthesiscommitteesForCommitteeMember4 = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommitteesforcommitteemember4.getThesisCommitteeId());

		// copy into the existing record to preserve existing relationships
		if (existingthesiscommitteesForCommitteeMember4 != null) {
			existingthesiscommitteesForCommitteeMember4.setThesisCommitteeId(related_thesiscommitteesforcommitteemember4.getThesisCommitteeId());
			related_thesiscommitteesforcommitteemember4 = existingthesiscommitteesForCommitteeMember4;
		} else {
			related_thesiscommitteesforcommitteemember4 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember4);
			thesiscommitteeDAO.flush();
		}

		related_thesiscommitteesforcommitteemember4.setFacultyByCommitteeMember1(faculty);
		faculty.getThesiscommitteesForCommitteeMember1().add(related_thesiscommitteesforcommitteemember4);
		related_thesiscommitteesforcommitteemember4 = thesiscommitteeDAO.store(related_thesiscommitteesforcommitteemember4);
		thesiscommitteeDAO.flush();

		faculty = facultyDAO.store(faculty);
		facultyDAO.flush();

		return faculty;
	}
}
